package com.dhcc.sdc.cdss.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.dhcc.cdc.busi.dao.CdcOrdmastMapper;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.cdc.busi.service.ICdcDictionaryService;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sdc.base.dao.SdcBaseAssessmentMapper;
import com.dhcc.sdc.base.entity.SdcBaseAssessmentEntity;
import com.dhcc.sdc.cdss.dao.SdcCdssItemMapper;
import com.dhcc.sdc.cdss.dto.SdcBaseAssessmentDto;
import com.dhcc.sdc.cdss.entity.SdcCdssItemEntity;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.sdc.cdss.dao.SdcCdssItemMatchMapper;
import com.dhcc.sdc.cdss.service.ISdcCdssItemMatchService;
import com.dhcc.sdc.cdss.entity.SdcCdssItemMatchEntity;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * CDSS推荐方案项目与实际项目对照表数据服务层接口
 * 
 * @ClassName: SdcCdssItemMatchServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2023-01-03 14:23:49
 */
@Service("sdcCdssItemMatchService")
public class SdcCdssItemMatchServiceImpl extends BaseServiceImpl<SdcCdssItemMatchMapper, SdcCdssItemMatchEntity> implements ISdcCdssItemMatchService {

	@Resource
	private SdcCdssItemMapper sdcCdssItemMapper;
	@Resource
	private CdcOrdmastMapper cdcOrdmastMapper;
	@Autowired
	private ICdcDictionaryService cdcDictionaryService;
	@Resource
	private SdcBaseAssessmentMapper sdcBaseAssessmentMapper;

	@Override
	public List<SdcCdssItemMatchEntity> page(Page<SdcCdssItemMatchEntity> page, Map<String, Object> map) {
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdcCdssItemMatchEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	public List<CdcOrdmastDto> pageMatchOrder(Page<SdcCdssItemMatchEntity> page, Map<String, Object> params) {
		List<CdcOrdmastDto> cdcOrdmastDtoList = CollUtil.newArrayList();
		String id = (String) params.get("id");
		String orderKey = "";
		if (!StringUtils.isEmpty(params.get("orderKey"))){
			orderKey = (String) params.get("orderKey");
		}
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(Long.parseLong(id));
		if (!StringUtils.isEmpty(sdcCdssItemEntity)){
			params.put("itemId", sdcCdssItemEntity.getItemId());
			params.put("entityId", sdcCdssItemEntity.getQcentityId());
		}else {
			return null;
		}
		//默认查询已对照的
		List<SdcCdssItemMatchEntity> page1 = this.baseMapper.page(page, params);
		if (null != page1 && page1.size() > 0){
			for (SdcCdssItemMatchEntity cdssItemMatchEntity : page1){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				CdcOrdmastEntity cdcOrdmastEntity = cdcOrdmastMapper.selectById(cdssItemMatchEntity.getMatchId());
				if (!StringUtils.isEmpty(orderKey) && !cdcOrdmastEntity.getDescription().contains(orderKey)){
					continue;
				}
				BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
				cdcOrdmastDto.setIsDefault(cdssItemMatchEntity.getIsDefault());
				if (CommonUtil.isNotEmpty(cdcOrdmastEntity) && !StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
					if (!StringUtils.isEmpty(cdcDictionaryEntity)){
						cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
					}
					if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
						CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
						if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
							cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
						}
					}
				}
				cdcOrdmastDto.setChecked(1);
				cdcOrdmastDtoList.add(cdcOrdmastDto);
			}
		}

		return cdcOrdmastDtoList;
	}

	@Override
	public List<CdcOrdmastDto> pageOrder(Page<CdcOrdmastEntity> page, Map<String, Object> params) {
		List<CdcOrdmastDto> res = CollUtil.newArrayList();
		String compare = (String) params.get("compare");
		String id = (String) params.get("id");
		String orderKey = "";
		if (!StringUtils.isEmpty(params.get("orderKey"))){
			orderKey = (String) params.get("orderKey");
		}
		params.put("description", orderKey);
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(Long.parseLong(id));
		if (!StringUtils.isEmpty(sdcCdssItemEntity)){
			params.put("itemId", sdcCdssItemEntity.getItemId());
		}else {
			return null;
		}

		if (Objects.equals("all", compare)){
			//查询所有
			List<SdcCdssItemMatchEntity> list = this.baseMapper.list(params);
			List<CdcOrdmastEntity> page1 = cdcOrdmastMapper.page(page, params);
			for (CdcOrdmastEntity cdcOrdmastEntity : page1){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
				if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
					if (!StringUtils.isEmpty(cdcDictionaryEntity)){
						cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
					}
					if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
						CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
						if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
							cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
						}
					}
				}
				res.add(cdcOrdmastDto);
			}
			if (list.size() == 0){
				return res;
			}else {
				for (CdcOrdmastDto cdcOrdmastDto : res){
					for (SdcCdssItemMatchEntity cdssItemMatchEntity : list) {
						if (Objects.equals(cdcOrdmastDto.getId(), cdssItemMatchEntity.getMatchId())) {
							cdcOrdmastDto.setChecked(1);
							cdcOrdmastDto.setIsDefault(cdssItemMatchEntity.getIsDefault());
						}
					}
				}
				return res;
			}
		}else if (Objects.equals("noMatch", compare)){
			//查询未对照的
			List<Long> matchOrdmastIds = this.baseMapper.selectMatchIds(params);
			List<CdcOrdmastEntity> cdcOrdmastEntities = cdcOrdmastMapper.pageExcludeMatch(page, params, matchOrdmastIds);
			for (CdcOrdmastEntity cdcOrdmastEntity : cdcOrdmastEntities){
				CdcOrdmastDto cdcOrdmastDto = new CdcOrdmastDto();
				BeanUtil.copyProperties(cdcOrdmastEntity,cdcOrdmastDto);
				if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderCateId())){
					CdcDictionaryEntity cdcDictionaryEntity = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderCateId());
					if (!StringUtils.isEmpty(cdcDictionaryEntity)){
						cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription());
					}
					if (!StringUtils.isEmpty(cdcOrdmastEntity.getBusOrderSubcateId())){
						CdcDictionaryEntity cdcDictionaryEntity2 = cdcDictionaryService.selectById(cdcOrdmastEntity.getBusOrderSubcateId());
						if (!StringUtils.isEmpty(cdcDictionaryEntity2)){
							cdcOrdmastDto.setOrderTypeDesc(cdcDictionaryEntity.getDescription() + "." + cdcDictionaryEntity2.getDescription());
						}
					}
				}
				res.add(cdcOrdmastDto);
			}
		}

		return res;
	}

	@Override
	public List<SdcBaseAssessmentDto> pageMatchAssessment(Page<SdcCdssItemMatchEntity> page, Map<String, Object> params) {
		List<SdcBaseAssessmentDto> baseAssessmentDtos = CollUtil.newArrayList();
		String id = (String) params.get("id");
		String orderKey = "";
		if (!StringUtils.isEmpty(params.get("orderKey"))){
			orderKey = (String) params.get("orderKey");
		}
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(Long.parseLong(id));
		if (!StringUtils.isEmpty(sdcCdssItemEntity)){
			params.put("itemId", sdcCdssItemEntity.getItemId());
			params.put("entityId", sdcCdssItemEntity.getQcentityId());
		}else {
			return null;
		}
		//默认查询已对照的
		List<SdcCdssItemMatchEntity> page1 = this.baseMapper.page(page, params);
		if (null != page1 && page1.size() > 0){
			for (SdcCdssItemMatchEntity cdssItemMatchEntity : page1){
				SdcBaseAssessmentDto baseAssessmentDto = new SdcBaseAssessmentDto();
				SdcBaseAssessmentEntity sdcBaseAssessmentEntity = sdcBaseAssessmentMapper.selectById(cdssItemMatchEntity.getMatchId());
				if(sdcBaseAssessmentEntity == null){
					continue;
				}
				if (!StringUtils.isEmpty(orderKey) && !sdcBaseAssessmentEntity.getDescription().contains(orderKey)){
					continue;
				}
				BeanUtil.copyProperties(sdcBaseAssessmentEntity,baseAssessmentDto);
				baseAssessmentDto.setChecked(1);
				baseAssessmentDto.setIsDefault(cdssItemMatchEntity.getIsDefault());
				baseAssessmentDtos.add(baseAssessmentDto);
			}
		}

		return baseAssessmentDtos;
	}

	@Override
	public List<SdcBaseAssessmentDto> pageAssessment(Page<SdcBaseAssessmentEntity> page, Map<String, Object> params) {
		List<SdcBaseAssessmentDto> res = CollUtil.newArrayList();
		String compare = (String) params.get("compare");
		String id = (String) params.get("id");
		String orderKey = "";
		if (!StringUtils.isEmpty(params.get("orderKey"))){
			orderKey = (String) params.get("orderKey");
		}
		params.put("description", orderKey);
		SdcCdssItemEntity sdcCdssItemEntity = sdcCdssItemMapper.selectById(Long.parseLong(id));
		if (!StringUtils.isEmpty(sdcCdssItemEntity)){
			params.put("itemId", sdcCdssItemEntity.getItemId());
		}else {
			return null;
		}
		if (Objects.equals("all", compare)){
			//查询所有
			List<SdcCdssItemMatchEntity> list = this.baseMapper.list(params);
			List<SdcBaseAssessmentEntity> page1 = sdcBaseAssessmentMapper.page(page, params);
			for (SdcBaseAssessmentEntity sdcBaseAssessmentEntity : page1){
				SdcBaseAssessmentDto sdcBaseAssessmentDto = new SdcBaseAssessmentDto();
				BeanUtil.copyProperties(sdcBaseAssessmentEntity,sdcBaseAssessmentDto);
				res.add(sdcBaseAssessmentDto);
			}
			if (list.size() == 0){
				return res;
			}else {
				for (SdcBaseAssessmentDto sdcBaseAssessmentDto : res){
					for (SdcCdssItemMatchEntity sdcCdssItemMatchEntity : list){
						if (Objects.equals(sdcBaseAssessmentDto.getId(), sdcCdssItemMatchEntity.getMatchId())){
							sdcBaseAssessmentDto.setChecked(1);
							sdcBaseAssessmentDto.setIsDefault(sdcCdssItemMatchEntity.getIsDefault());
						}
					}
				}
				return res;
			}
		}else if (Objects.equals("noMatch", compare)){
			//查询未对照的
			List<Long> matchAssessmentIds = this.baseMapper.selectMatchIds(params);
			List<SdcBaseAssessmentEntity> sdcBaseAssessmentEntities = sdcBaseAssessmentMapper.pageExcludeMatch(page, params, matchAssessmentIds);
			for (SdcBaseAssessmentEntity baseAssessmentEntity : sdcBaseAssessmentEntities){
				SdcBaseAssessmentDto sdcBaseAssessmentDto = new SdcBaseAssessmentDto();
				BeanUtil.copyProperties(baseAssessmentEntity,sdcBaseAssessmentDto);
				res.add(sdcBaseAssessmentDto);
			}
		}


		return res;
	}

}